#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define STACK_ELEM_MAX   100
#define URL_MAX_LEN 70
#define COMMAND_NUM 4
typedef struct tagStack
{
    int top;
    char url[STACK_ELEM_MAX][URL_MAX_LEN];
} STACK;

//命令
char command[COMMAND_NUM][10] = {"QUIT", "BACK", "FORWARD", "VISIT"};

STACK forwardStack;
STACK backStack;
char curPage[URL_MAX_LEN];

void initStack(STACK* stack)
{
    int i = 0;
    for(i = 0; i < STACK_ELEM_MAX; i++)
    {
        strcpy(stack->url[i], "");
    }
    stack->top = -1;
}

int isEmpty(STACK* stack)
{
    if(stack->top == -1)
        return 1;
    return 0;
}

void pushStack(STACK* stack, const char* strUrl)
{
    if(stack->top >= STACK_ELEM_MAX)
        exit(-1);
    strcpy(stack->url[++stack->top], strUrl);
}

void popStack(STACK* stack, char* strUrl)
{
    if(stack->top == -1)
        exit(-1);
    strcpy(strUrl, stack->url[stack->top--]);
}

char* getTop(STACK* stack)
{
    if(isEmpty(stack) == 1)
        return NULL;
    return stack->url[stack->top];
}

void clearStack(STACK* stack)
{
    initStack(stack);
}

int main()
{
    int i = 0, j = 0;
    int nFlag = -1;
    char strCmdLine[URL_MAX_LEN];
    char strCmd[10];
    char strUrl[URL_MAX_LEN];
    char strUrlCur[URL_MAX_LEN];
    char strMsg[URL_MAX_LEN];
//    freopen("bin\\debug\\in.txt", "r", stdin);
//   freopen("bin\\debug\\out.txt", "w", stdout);

    initStack(&forwardStack);
    initStack(&backStack);
    strcpy(strUrlCur, "http://www.acm.org/");

    while(1)
    {
        gets(strCmdLine);

        //printf("strcmdline:%s\r\n", strCmdLine);
        for(i = 0; strCmdLine[i] != ' ' && i < strlen(strCmdLine); i++)
            strCmd[i] = strCmdLine[i];
        strCmd[i++] = '\0';

        for(j = 0; i < strlen(strCmdLine); j++, i++)
            strUrl[j] = strCmdLine[i];
        strUrl[j] = '\0';

       //printf("cmd: %s %d url: %s %d\r\n", strCmd, strlen(strCmd), strUrl, strlen(strUrl));

        for(i = 0; i < COMMAND_NUM; i++)
        {
            if(strcmp(strCmd, command[i]) == 0)
            {
                nFlag = i;
                break;
            }
        }
//char command[COMMAND_NUM][10] = {"QUIT", "BACK", "FORWARD", "VISIT"};
        switch(nFlag)
        {
        case 0://QUIT
            exit(0);
        case 1://BACK
            if(isEmpty(&backStack) == 1)
            {
                strcpy(strMsg, "Ignored");
            }
            else
            {
                pushStack(&forwardStack, strUrlCur);
                popStack(&backStack, strUrlCur);
                sprintf(strMsg, "%s", strUrlCur);
            }
            break;

        case 2://FORWARD
            if(isEmpty(&forwardStack) == 1)
            {
                strcpy(strMsg, "Ignored");
                break;
            }
            {
                pushStack(&backStack,strUrlCur);
                popStack(&forwardStack, strUrlCur);
                sprintf(strMsg, "%s", strUrlCur);
            }

            break;

        case 3://VISIT
            clearStack(&forwardStack);
            pushStack(&backStack, strUrlCur);
            strcpy(strUrlCur, strUrl);
            sprintf(strMsg, "%s", strUrlCur);
            break;
        default:
            break;
        }

        printf("%s\r\n", strMsg);
    }
    return 0;
}
